package org.BUPT.domain.model.question;


import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import org.BUPT.domain.factory.RepoFactory;
import org.BUPT.domain.service.AnswerService;
import org.BUPT.domain.service.impl.AnswerServiceImpl;
import org.BUPT.infrastructure.exception.PersistException;
import org.BUPT.infrastructure.persistent.po.Answer;
import org.BUPT.infrastructure.persistent.po.Comment;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

@Getter
@Setter
public class AnswerBO extends Commentable{
    private int id;
    private int userId;
    private String content;
    private Date createTime;
    private Date editTime;
    private List<CommentBO> comments;



    private Set<Integer> likedUserId;
    private int questionId;



    @Getter(AccessLevel.NONE)
    private final AnswerService answerService = RepoFactory.get(AnswerServiceImpl.class);

    public AnswerBO() throws PersistException {

    }
    public AnswerBO(Answer answer) throws PersistException {
        List<Comment> comments = answerService.getCommentsOfAnswer(answer.getId());
        List<CommentBO> commentBOS = new ArrayList<>();
        for (Comment comment : comments){
            CommentBO commentBO = new CommentBO(comment);
            commentBOS.add(commentBO);
        }
        Set<Integer> likedUserId = answerService.getLikedUserId(answer.getId());
        this
                .setId(answer.getId())
                .setQuestionId(answer.getQuestionId())
                .setContent(answer.getContent())
                .setComments(commentBOS)
                .setCreateTime(answer.getCreateTime())
                .setEditTime(answer.getUpdateTime())
                .setUserId(answer.getUserId())
                .setLikedUserId(likedUserId);
    }



    //---------------------------------------------------------------------------
    public void save(){
        answerService.saveAnswer(

                new Answer()
                    .setId(getId())
                    .setContent(getContent())
                    .setUserId(getUserId())
                    .setQuestionId(getQuestionId())
                    .setCreateTime(getCreateTime())
                    .setUpdateTime(getEditTime())
                    .setIsDel(0)
                    .setScore(-1)
                    .setTimestamp(getEditTime()!=null?getEditTime():getCreateTime())

        );


    }
    public void delete(){
        // todo: 删除回答
    }
//    @JsonIgnore
//    public QuestionBO getParent() throws NullObjectException {
//        try{
//            return answerBOFactory.questionBOFactory.getQuestion(getQuestionId());
//        }
//        catch (NullObjectException e){
//            delete();
//            throw e;
//        }
//    }
    public void like(int userId){

        getLikedUserId().add(userId);
        answerService.likeAnswer(userId,id);
    }
    public void dislike(int userId){
        getLikedUserId().remove(userId);
        answerService.dislikeAnswer(userId,id);
    }
    public boolean isLiked(int userId){
        return getLikedUserId().contains(userId);
    }

}

